﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;
using PDBSmartMVVM.model;

namespace PDBSmartMVVM.viewmodel
{
    public class AminoTreeViewModel : TreeViewItemViewModel
    {
        public List<LinesVisual3D> ChemicalBondsCollection { get; set; }
        public Amino Amino { get; set; }
        public TreeViewItemViewModel Parent { get; set; }
        public ObservableCollection<object> Objects { get; set; }
        public AminoTreeViewModel(TreeViewItemViewModel parent, Amino amino, ObservableCollection<object> objects) : base(parent, false)
        {
            Amino = amino;
            Parent = parent;
            Objects = objects;
            foreach (Atom atom in Amino.Atoms.Values)
            {
                base.Children.Add(new AtomViewModel(this, atom, Objects) { IsChecked = false });
            }
            ChemicalBondsCollection = new List<LinesVisual3D>();
            base.PropertyChanged += OnPropertyChanged;
        }

        private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
        {
            if (propertyChangedEventArgs.PropertyName == "IsChecked")
            {
                var vm = (AminoTreeViewModel)sender;
                if (vm.IsChecked == true)
                {
                    foreach (var line in Utils.DrawAnino(Amino))
                    {
                        ((ModelTreeViewModel)Parent).Objects.Add(line);
                        ChemicalBondsCollection.Add(line);
                    }
                    ((ModelTreeViewModel) Parent).CheckAmino(Amino.Index);
                }
                else
                {
                    IsCalculated = false;
                    foreach (Atom atom in Amino.Atoms.Values)
                    {
                        atom.ModelUiElement3D = null;
                    }
                    foreach (var line in ChemicalBondsCollection)
                    {
                        ((ModelTreeViewModel)Parent).Objects.Remove(line);
                    }
                    ChemicalBondsCollection.Clear();
                    ((ModelTreeViewModel)Parent).UnCheckAmino(Amino.Index);


                }   
            }
            if (propertyChangedEventArgs.PropertyName == "IsCalculated")
            {
                ((AtomViewModel)Children[0]).ChangeCalculated();
            }
        }

        private RelayCommand _clearAminoCommand;
        public ICommand ClearAminoCommand
        {

            get { return _clearAminoCommand ?? (_clearAminoCommand = new RelayCommand(ClearAminoCommandExecute)); }
        }

        private void ClearAminoCommandExecute(object o)
        {
            this.IsCalculated = false;
        }

        private RelayCommand _selectAminoCommand;
        public ICommand SelectAminoCommand
        {

            get { return _selectAminoCommand ?? (_selectAminoCommand = new RelayCommand(SelectAminoCommandExecute)); }
        }

        private void SelectAminoCommandExecute(object o)
        {
            this.IsCalculated = true;
        }
    }
}
